package com.loyal.loyalerp.humenresource.kaoqinMonth.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.loyal.loyalerp.commons.ServerResponse;
import com.loyal.loyalerp.humenresource.askleave.model.AskLeave;
import com.loyal.loyalerp.humenresource.kaoqinMonth.model.RsKaoqinmonth;
import com.loyal.loyalerp.humenresource.kaoqinMonth.service.KaoqinMonthService;
import com.loyal.loyalerp.model.GyPerson;
import com.loyal.loyalerp.model.vo.GridVO;
import com.loyal.loyalerp.model.vo.Result;
import com.loyal.loyalerp.utils.ValidateUtil;

@Controller
@RequestMapping("/kaoqinmonth")
public class KaoqinMonthController {
	
	final static String permission = "rs_kaoqinmonth";
	
	@Autowired
	KaoqinMonthService kaoqinMonthService;

	@RequestMapping("jsp")
	public String catt() {
		return "HumenResource/KaoqinMonth/kaoqinMonth";
	}
	
	@RequestMapping("closemonth")
	public String closejsp() {
		return "HumenResource/KaoqinMonth/closemonth";
	}
	
	@RequestMapping("search")
	public String searchjsp() {
		return "HumenResource/KaoqinMonth/search";
	}
	
	/**
	 * 初始化
	 * 初始化当年1月到12月
	 * 
	 * 注意：
	 * 1.判断某月份是否已经被初始化
	 * 2.如果被初始化过，就提示已初始化，否则就开始初始化
	 * 3.如果上一年没有全部月结，就不能初始化下一年
	 */
	@RequestMapping("/start")
	public @ResponseBody Result start(String companyname,String years) {
		Result result = new Result();
		if (!ValidateUtil.init(permission)) {
			result.setStatus(444);
			return result;
		}
		
		int companyId=kaoqinMonthService.selectCompanyIdByCompanyName(companyname);
		//如果上一年没有全部月结，就不能初始化这一年
		System.out.println(companyId+" "+years);
		boolean b=kaoqinMonthService.judge(companyId, Integer.parseInt(years)-1);
		if(!b) {
			result.setStatus(222);
			return result;
		}
		RsKaoqinmonth km=new RsKaoqinmonth();
		km.setComopanyid(companyId);
		km.setYear(Integer.parseInt(years));
		int rs=kaoqinMonthService.selectByCY(km);
		//未被初始化过
		if(rs==0) {
			try {
				kaoqinMonthService.start(km);
				result.setStatus(200);
			}catch(Exception e) {
				result.setStatus(500);
			}
		//已被初始化	
		}else {
			result.setStatus(400);
		}
		//
		return result;
	}
	
	@ResponseBody
	@RequestMapping("/listKaoqinMonth")
	public Object listKaoqinMonth(String _search,Integer page, Integer rows, String sidx,String sord,String searchField,String searchString) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
		
		if (!ValidateUtil.validateQuery(permission)) {
			return ServerResponse.createByError("没有权限");
		}
		
		if("false".equals(_search)) {
			PageInfo<RsKaoqinmonth> pageInfo = kaoqinMonthService.listKaoqinMonth(page, rows, sidx, sord);
			return GridVO.fromPageInfo(pageInfo);
		}else {
			PageInfo<RsKaoqinmonth> pageInfo = kaoqinMonthService.findKm(page, rows, searchField, searchString);
			return GridVO.fromPageInfo(pageInfo);
		}
	}
	
	
	/**
	 * 月结操作：
	 * 1.检查请假申请表，是否有该月未审核的请假，如果有就提示
	 * 2.如果没有，先根据请假申请表，统计数据到考勤表，然后将月结状态改为已月结
	 * 注意：
	 * 这个月月结要判断上个月是否已经月结，如果上个月已经月结过，那么可以月结操作，如果没有月结上个月，提示先月结上个月
	 */
	@RequestMapping("/doClosemonth")
	public @ResponseBody Result doClosemonth(String comopanyid,String year,String month) {
		Result result=new Result();
		if (!ValidateUtil.closeMonth(permission)) {
			result.setStatus(777);
			return result;
		}
		int yearInt=Integer.parseInt(year);
		int monthInt=Integer.parseInt(month);
		int companyId=kaoqinMonthService.selectCompanyIdByCompanyName(comopanyid);
		RsKaoqinmonth km=null;
		//根据year+month得到这个月的所有请假记录
		//根据personId查companyId
		//companyId+personId+KjYear+Period+Days插入到考勤表中
		//将该月的月结状态改为已月结(mCloseFlag=1)
		
		//先判断上个月有没有月结
		//上一年没有初始化，这一年的第一月份
		if(monthInt-1 == 0 ) {
			int yearInt2=yearInt-1;
			int monthInt2=12;
			km=new RsKaoqinmonth(companyId, yearInt2, monthInt2);
			System.out.println("进来了!");
			if(kaoqinMonthService.selectByCYM(km)==null) {
				kaoqinMonthService.insertPreviousYear(km);
			}
		//上一个月还是本年
		}else {
			int previousMonth=monthInt-1;
			km=new RsKaoqinmonth(companyId, yearInt, previousMonth);
			int closeFlag=kaoqinMonthService.selectCloseFlag(km);
			if(closeFlag==0) {
				result.setStatus(600);
			}
			return result;
		}
		System.out.println("yearInt:"+yearInt+" monthInt"+monthInt);
		//查询请假表中的记录，看请假记录是否审核通过
		List<AskLeave> askList=kaoqinMonthService.checkAsk(yearInt, monthInt);
		int count=0;
		for(AskLeave ask : askList) {
			if(ask.getCheckerId()==1) {
				count++;
			}
		}
		System.out.println("count:"+count);
		//已审核
		if(count==askList.size() && count!=0) {
			for(AskLeave ask : askList) {
				GyPerson person=kaoqinMonthService.getPerson(ask.getPersonId());
				companyId=person.getCompanyid().intValue();
				ask.setKjYear(yearInt);
				ask.setPeriod(monthInt);
				try {
					//将请假表中的请假天数添加到考勤表中
					kaoqinMonthService.updateByCPYPD(companyId, ask);
					//更新mCloseFlag为1
					kaoqinMonthService.updateCloseFlag(companyId, yearInt, monthInt);
					
				}catch(Exception e) {
					//添加不成功或者，已月结  
					result.setStatus(400);
					return result;
				}
			}
			//月结成功
			result.setStatus(200);
		//请假表中有未审核的记录	
		}else {
			result.setStatus(500);
		}
		
		return result;
	}
	
	
}
